home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / mkfifo / mkfifo.c < prev    next >
C/C++ Source or Header  |  1997-08-20  |  5KB  |  190 lines

  1. /*
  2. ** ****************************************************************************
  3. ** This implements the mkfifo command under OS/2.
  4. ** (c) Klaus Gebhardt, 1997
  5. ** ****************************************************************************
  6. */
  7.  
  8. /*
  9. ** ****************************************************************************
  10. ** This was written for the OS/2 port of Octave, but it is not part of Octave!
  11. ** You can use the code UNMODIFIED. If you think changes are necessary,
  12. ** please send me a mail (gebhardt@crunch.ikp.physik.th-darmstadt.de).
  13. ** Thanks,
  14. **   Klaus Gebhardt
  15. ** ****************************************************************************
  16. */
  17.  
  18. #include "mkfifo.h"
  19. #include <process.h>
  20. #include <stdio.h>
  21.  
  22. #define INCL_DOSNMPIPES
  23. #define INCL_DOSPROCESS
  24. #include <os2.h>
  25.  
  26.  
  27. int mkfifo (const char *pathname, mode_t mode)
  28. {
  29.   APIRET rc;
  30.   HPIPE  hpipe;
  31.   ULONG  action;
  32.   ULONG  count;
  33.  
  34.   int mkfifo_cmd = _MKFIFO_CREATE_NMP_;
  35.   int result = 0;
  36.   int pid = -1;
  37.  
  38.   do
  39.     {
  40.       rc = DosWaitNPipe (_MKFIFO_NAMED_PIPE_, 0xFF);
  41.  
  42.       if ((rc == 3) && (pid == -1))
  43.     {
  44.       pid = spawnlp (P_DETACH, "nmpserv", "nmpserv", NULL);
  45.       if (pid == -1)  return -1;
  46.     }
  47.       else if (rc == 0)
  48.     {
  49.       rc = DosOpen (_MKFIFO_NAMED_PIPE_, &hpipe, &action, 0, 0,
  50.             OPEN_ACTION_OPEN_IF_EXISTS,
  51.             OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR |
  52.             OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
  53.             OPEN_ACCESS_READWRITE, 0);
  54.     }
  55.     }
  56.   while (rc);
  57.  
  58.   rc = DosWrite (hpipe, &mkfifo_cmd, sizeof (mkfifo_cmd), &count);
  59.   if (rc || (count != sizeof (mkfifo_cmd)))  goto exit;
  60.  
  61.   DosWrite (hpipe, &mode, sizeof (mode), &count);
  62.   if (rc || (count != sizeof (mode)))  goto exit;
  63.  
  64.   DosWrite (hpipe, pathname, strlen (pathname) + 1, &count);
  65.   if (rc || (count != strlen (pathname) + 1))  goto exit;
  66.  
  67.   rc = DosRead (hpipe, &result, sizeof (result), &count);
  68.   if (rc || (count != sizeof (result)))  goto exit;
  69.  
  70. exit:
  71.   DosClose (hpipe);
  72.   return result;
  73. }
  74.  
  75.  
  76. int remove_mkfifo (const char *pathname)
  77. {
  78.   APIRET rc;
  79.   HPIPE  hpipe;
  80.   ULONG  action;
  81.   ULONG  count;
  82.  
  83.   int mkfifo_cmd = _MKFIFO_REMOVE_NMP_;
  84.   int result = 0;
  85.  
  86.   rc = DosOpen (_MKFIFO_NAMED_PIPE_, &hpipe, &action, 0, 0,
  87.         OPEN_ACTION_OPEN_IF_EXISTS,
  88.         OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR |
  89.         OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
  90.         OPEN_ACCESS_READWRITE, 0);
  91.  
  92.   if (rc)  return 0;
  93.  
  94.   rc = DosWrite (hpipe, &mkfifo_cmd, sizeof (mkfifo_cmd), &count);
  95.   if (rc || (count != sizeof (mkfifo_cmd)))  goto exit;
  96.  
  97.   DosWrite (hpipe, pathname, strlen (pathname) + 1, &count);
  98.   if (rc || (count != strlen (pathname) + 1))  goto exit;
  99.  
  100.   rc = DosRead (hpipe, &result, sizeof (result), &count);
  101.   if (rc || (count != sizeof (result)))  goto exit;
  102.  
  103. exit:
  104.   DosClose (hpipe);
  105.   return result;
  106. }
  107.  
  108.  
  109. int list_named_pipes ()
  110. {
  111.   APIRET rc;
  112.   HPIPE  hpipe;
  113.   ULONG  action;
  114.   ULONG  count;
  115.  
  116.   int mkfifo_cmd = _MKFIFO_LIST_NMPS_;
  117.   int i, n;
  118.   char name[1024];
  119.  
  120.   rc = DosOpen (_MKFIFO_NAMED_PIPE_, &hpipe, &action, 0, 0,
  121.         OPEN_ACTION_OPEN_IF_EXISTS,
  122.         OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR |
  123.         OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
  124.         OPEN_ACCESS_READWRITE, 0);
  125.  
  126.   if (rc)  return 0;
  127.  
  128.   rc = DosWrite (hpipe, &mkfifo_cmd, sizeof (mkfifo_cmd), &count);
  129.   if (rc || (count != sizeof (mkfifo_cmd)))  goto exit;
  130.  
  131.   rc = DosRead (hpipe, &n, sizeof (n), &count);
  132.   if (rc || (count != sizeof (n)))  goto exit;
  133.  
  134.   for (i=0; i<n; i++)
  135.     {
  136.       rc = DosWrite (hpipe, &i, sizeof (i), &count);
  137.       if (rc || (count != sizeof (i)))  goto exit;
  138.  
  139.       rc = DosRead (hpipe, name, sizeof (name), &count);
  140.       if (rc)  goto exit;
  141.  
  142.       printf ("%3i: %s\n", i, name);
  143.     }
  144.  
  145.   DosClose (hpipe);
  146.   return 0;
  147.  
  148. exit:
  149.   DosClose (hpipe);
  150.   return -1;
  151. }
  152.  
  153.  
  154. int exit_server ()
  155. {
  156.   APIRET rc;
  157.   HPIPE  hpipe;
  158.   ULONG  action;
  159.   ULONG  count;
  160.  
  161.   int mkfifo_cmd = _MKFIFO_EXIT_SERVER_;
  162.   int n;
  163.  
  164.   rc = DosOpen (_MKFIFO_NAMED_PIPE_, &hpipe, &action, 0, 0,
  165.         OPEN_ACTION_OPEN_IF_EXISTS,
  166.         OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR |
  167.         OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE |
  168.         OPEN_ACCESS_READWRITE, 0);
  169.  
  170.   if (rc)  return 0;
  171.  
  172.   rc = DosWrite (hpipe, &mkfifo_cmd, sizeof (mkfifo_cmd), &count);
  173.   if (rc || (count != sizeof (mkfifo_cmd)))  goto exit;
  174.  
  175.   rc = DosRead (hpipe, &n, sizeof (n), &count);
  176.   if (rc || (count != sizeof (n)))  goto exit;
  177.  
  178.   if (n > 1)
  179.     fprintf (stderr, "\nwarning: %i named pipes destroyed.", n);
  180.   else if (n == 1)
  181.     fprintf (stderr, "\nwarning: %i named pipe destroyed.", n);
  182.  
  183.   DosClose (hpipe);
  184.   return 0;
  185.  
  186. exit:
  187.   DosClose (hpipe);
  188.   return -1;
  189. }
  190.